require('./Listclassitem.styl');
import { Boxs, Group } from 'saltui';
import { Control } from 'react-keeper'
import FaceSmileFull from 'salt-icon/lib/FaceSmileFull';
import CheckRound from 'salt-icon/lib/CheckRound';
const { HBox, Box, VBox } = Boxs;
const Stay = require('images/svg/stay.svg');
const NoStay = require('images/svg/nostay.svg');
const alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"];
class Listclassitem extends React.Component {

    constructor(props) {
        super(props);
        let { a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z } = props;
        let obj = { a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z };
        for (let [key, value] of Object.entries(obj)) {
            if (!value) {
                delete obj[key];
            }
        }
        this.state = {
            listdata: Object.values(obj) || [],
            all_select: false
        };
        this.handleClickItem = this.handleClickItem.bind(this);
        this.handleClicIcon = this.handleClicIcon.bind(this);
        window.groupEl = {};
        this.selectId = [];
    }
    handleClickItem(region, index) {
        if (this.props.refreshing) return;
        if (this.props.origin == "menu") return;
        if (region) {
            let { schoolId, id: studentId } = this.props[region][index];
            Control.go('/home/personalinfo', { data: { schoolId, studentId } })
        }

    }
    handleClicIcon(e,studentId) {
        e.stopPropagation();
        let id = [`active${studentId}`];
        let values = (eval(`!this.state.${id}`));
        this.setState({ [`active${studentId}`]: values }, () => {
            if (values) {
                this.selectId.push(studentId);
                if (!this.state.all_select && this.selectId.length == this.state.listdata[0].length) {
                    this.props.handleCb && this.props.handleCb(true)
                }
            } else {
                const fn = (item) => {
                    return item == studentId;
                }
                let num = this.selectId.findIndex(fn);
                if (num >= 0) {
                    this.selectId.splice(num, 1);
                }
                if (this.state.all_select && this.selectId.length != this.state.listdata[0].length) {
                    this.props.handleCb && this.props.handleCb(false)
                }
            }
        })
    }
    render() {
        let t = this;
        return (
            <div className="listclassitem" ref="listclassitem">
                {this.state.listdata && this.state.listdata.length > 0 && this.state.listdata.map((item, index) => {
                    if (!item) return null;
                    return (
                        <Group >
                            <div className="t-LH30 t-PL10 t-head" ref={(ref) => { window.groupEl[alphabet[index].toUpperCase()] = ref; }}>
                                {alphabet[index].toUpperCase()}
                            </div>
                            {item.map((subitem, subindex) => {
                                return (
                                    <Group.List className="t-LH44 t-PL10 t-head-item">
                                        <HBox vAlign="center" className="t-FAL" onClick={this.handleClickItem.bind(this, alphabet[index], subindex)}>
                                            <Box className="">
                                                {subitem.inResidence == '1' ?
                                                    <Stay
                                                        fill=""
                                                        width="20"
                                                        height="20" />
                                                    :
                                                    <NoStay
                                                        fill=""
                                                        width="20"
                                                        height="20" />
                                                }
                                            </Box>
                                            <Box flex={3} className="t-ML15">
                                                {subitem.name}
                                            </Box>
                                            {this.props.origin == "menu" &&
                                                <Box flex={1} className="t-MR30 t-FAR">
                                                    {!t.state[`active${subitem.id}`] ? <span className="circle" data-id={subitem.id} onClick={(e)=>{this.handleClicIcon(e,subitem.id)}}></span> :
                                                        <CheckRound
                                                            onClick={(e)=>{this.handleClicIcon(e,subitem.id)}}
                                                            fill="rgb(14,98,181)"
                                                            width="20"
                                                            height="20"
                                                        />}
                                                </Box>
                                            }
                                            {this.props.isBzr && subitem.passable == 1 &&
                                                < Box flex={1} className="t-MR30 t-FAR text-color">
                                                    已允许
                                                </Box>
                                            }
                                        </HBox>
                                    </Group.List>
                                );
                            })}
                        </Group>
                    );
                })}
            </div>
        );
    }

    componentWillMount() {
    }

    componentDidMount() {
    }

    componentWillReceiveProps(nextProps) {       
        //#warning
        if (nextProps.data) {
            let { a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z } = this.props;
            let obj = { a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z };
            let arr = Object.values(obj)
            this.setState({ listdata: arr })
        }
        if (this.props.origin == "menu" && nextProps.all_select != this.state.all_select) {
            this.setState({ all_select: nextProps.all_select });
            if(this.selectId.length == this.state.listdata[0].length||this.selectId.length==0){
                this.selectId = [];
                for(let item of this.state.listdata){
                    for(let subitem of item){
                        this.setState({[`active${subitem.id}`]:nextProps.all_select});
                        if(nextProps.all_select){
                            this.selectId.push(subitem.id)
                        }         
                    }
                }
            }
        }
    }

    shouldComponentUpdate(nextProps, nextState) {
        return true;
    }

    componentWillUpdate(nextProps, nextState) {
    }

    componentDidUpdate(prevProps, prevState) {
    }

    componentWillUnmount() {
    }
}

module.exports = Listclassitem;
